function WeightedPollItem(obj)
{
  this.obj = obj;
  this.name = this.obj['name'];

  this.getVoteButton = function()
  {
    var score = this.getScore(wave.getViewer().getId());
    if(!score) score = "";

    var button = newElement("input", {type: 'text', className: 'textVoteButton', value: score});
    button.title = 'Enter your score';
    $(button).bind('change', {item:this}, function(ev) {
			score = parseInt(this.value);
      var validInput = false;
      if(score < 0) score = 0;
      else if(score > 10) score = 10;
      else validInput = ! isNaN(score);

      if(!validInput) errorMessage("Please enter a number between 0 and 10");
			ev.data.item.setScore(score);
        })

    return button;
  }

  this.getNumVotes = function()
  {
    var score = 0;
    var item = this;
    $(wave.getParticipants()).each(function(i,p){
      score += item.getScore(p.getId());
        });
    return score;
  }

  this.getVoters = function()
  {
    var item = this;
    
    return jQuery.grep(wave.getParticipants(), function(p)
        { return item.getScore(p.getId()) != null; });
  }

  this.getScore = function(id)
  {
    var userData = getStateValue(id);
    if(!userData) return null;
    return userData[this.name];
  }

  this.setScore = function(score)
  {
    var id = wave.getViewer().getId();
    userData = getStateValue(id) || {};

    if(userData[this.name] == score) return; // Don't send dups

    userData[this.name] = score;
    submitValue(id, userData);
    
  }
}

function renderPoll(state)
{
  renderTable(state, WeightedPollItem);
}


